import os

from calc_v1.calcQ import calc_gep_main, batch_replace_negative_with_zero, run_matrix_calculator_noEco
from calc_v1.formulas import formulas_list_V
from tif_stats import analyze_tif_files, ett_cost_type_mapping, fm_cost_type_mapping, \
    wr_cost_type_mapping

def vsr_calculate(vsr_folder, json_folder_path):
    # 计算Vsr
    os.makedirs(vsr_folder, exist_ok=True)
    # 获取文件夹中的所有 JSON 文件
    json_files = [f for f in os.listdir(json_folder_path) if f.endswith('.json')]
    for json_file in json_files:
        json_file_path = os.path.join(json_folder_path, json_file)
        print(f"正在处理文件: {json_file_path}")
        run_matrix_calculator_noEco(json_file_path, "v_sr_variables", formulas=formulas_list_V,
                                    output_base_path=output_base_path_vsr)

    analyze_tif_files(output_base_path_vsr, cost_type_mapping=vsr_cost_type_mapping,
                      excel_path=r'H:\GEP_excel_data\V_price\价格表-总表.xlsx')

if __name__ == '__main__':
    # import time
    # delay_seconds = 3600 * 8   # 2小时 = 7200秒
    #
    # # 在调用函数之前暂停程序
    # print(f"程序将在 {delay_seconds / 3600} 小时后开始执行...")
    # time.sleep(delay_seconds)

    # 定义 JSON 文件夹路径
    json_folder_path = r'F:\code\dev\calc-gep-regulate-cqc\calc_v1\data\2024\para_json'

    # 定义输出路径
    output_base_paths = {
        # "ap_variables": r'I:\GEP_result\2024test\Qap_main',
        # "wp_variables": r'I:\GEP_result\2024test\Qwp_main',
        # "tco2_variables": r'I:\GEP_result\2024test\Qtco2_main',
        "fm_variables": r'I:\GEP_result\2024test\Qfm_main',
        "ett_variables": r'I:\GEP_result\2024test\Qett_main',
        "wr_variables": r'I:\GEP_result\2024test\Qwr_main',
        "sr_variables": r'I:\GEP_result\2024test\Qsr_main',
        # "sf_variables": r'I:\GEP_result\2024test\Qsf_main',
    }
    calc_gep_main(json_folder_path=json_folder_path, output_base_paths=output_base_paths, max_workers=1)

    # 根据 output_base_paths 自动生成 cost_type_mappings
    cost_type_mappings = {}
    for key in output_base_paths.keys():
        cost_type_mappings[key] = key.split('_')[0] + "_cost_type_mapping"

    for key, cost_type_mapping in cost_type_mappings.items():
        if key in output_base_paths:  # 检查 output_base_paths 是否有对应的键
            if cost_type_mapping == "fm_cost_type_mapping":
                from tif_stats import fm_cost_type_mapping

                cost_type_mapping = fm_cost_type_mapping
            elif cost_type_mapping == "ett_cost_type_mapping":
                from tif_stats import ett_cost_type_mapping

                cost_type_mapping = ett_cost_type_mapping
            elif cost_type_mapping == "wr_cost_type_mapping":
                from tif_stats import wr_cost_type_mapping

                cost_type_mapping = wr_cost_type_mapping
            elif cost_type_mapping == "sr_cost_type_mapping":
                from tif_stats import sr_cost_type_mapping

                cost_type_mapping = sr_cost_type_mapping
            elif cost_type_mapping == "vsr_cost_type_mapping":
                from tif_stats import vsr_cost_type_mapping

                cost_type_mapping = vsr_cost_type_mapping
            elif cost_type_mapping == "sf_cost_type_mapping":
                from tif_stats import sf_cost_type_mapping

                cost_type_mapping = sf_cost_type_mapping
            elif cost_type_mapping == "vsf_cost_type_mapping":
                from tif_stats import vsf_cost_type_mapping

                cost_type_mapping = vsf_cost_type_mapping
            elif cost_type_mapping == "ap_cost_type_mapping":
                from tif_stats import ap_cost_type_mapping

                cost_type_mapping = ap_cost_type_mapping
            elif cost_type_mapping == "wp_cost_type_mapping":
                from tif_stats import wp_cost_type_mapping

                cost_type_mapping = wp_cost_type_mapping
            elif cost_type_mapping == "tco2_cost_type_mapping":
                from tif_stats import tco2_cost_type_mapping

                cost_type_mapping = tco2_cost_type_mapping
            print(f"正在分析 {key} 文件...", cost_type_mapping)
            analyze_tif_files(output_base_paths[key], cost_type_mapping=cost_type_mapping,
                              excel_path=r'H:\GEP_excel_data\V_price\价格表-总表.xlsx')

    wr_zero_folder = os.path.join(output_base_paths["wr_variables"], "zero")
    batch_replace_negative_with_zero(output_base_paths["wr_variables"], wr_zero_folder, num_processes=1)
    analyze_tif_files(wr_zero_folder, cost_type_mapping=wr_cost_type_mapping, excel_path=r'H:\GEP_excel_data\V_price\价格表-总表.xlsx')

    output_base_path_vsr = r'I:\GEP_result\2024test\Vsr_main'
    vsr_calculate(vsr_folder=output_base_path_vsr, json_folder_path=json_folder_path)

